﻿using Dora.Tools.EContract.Dto;
using Dora.Tools.Utility;
using Dora.Tools.Utility.Const.ApiSetting;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Dora.Tools.EContract
{
    /// <summary>
    /// 电子合同帮助类
    /// </summary>
    public class EContractHelper
    {
        /// <summary>
        /// 申报电子合同
        /// </summary>
        /// <param name="contractDto">申报合同的数据结构</param>
        /// <param name="contractNumber">表示可选的合同编号参数。如果合同编号参数有值，则走重新签署的流程，否则走新建的流程</param>
        /// <returns></returns>
        public static EContractResponseDto ApplyContract(EContractDto contractDto, BaseRequest baseres, string contractNumber = "")
        {
            RestRequest request = new RestRequest("/v1/econtract/apply", Method.POST);
            SetHeaders(request, baseres);
            request.AddQueryParameter("contractNumber", contractNumber);
            request.AddJsonBody(contractDto);
            var dto = RestHelper.Execute<EContractResponseDto>(EContractConstValue.BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 申报补充协议
        /// 按主合同编号申报补充协议，可自定义协议签署人，默认为主合同的签署人、经办人等信息，并发送短信
        /// </summary>
        /// <param name="requestDto">补充协议请求参数</param>
        /// <param name="contractNumber">电子合同编号</param>
        public static SupplementResponseDto SupplementAgreement(SupplementRequestDto requestDto, string contractNumber)
        {
            RestRequest request = new RestRequest("/v1/econtract/sa/apply", Method.POST);
            SetHeaders(request, requestDto);
            request.AddQueryParameter("contractNumber", contractNumber);
            request.AddJsonBody(requestDto);
            var dto = RestHelper.Execute<SupplementResponseDto>(EContractConstValue.BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 按合同编号获取合同的基础信息
        /// </summary>
        /// <param name="contractNumber">电子合同编号</param>
        /// <returns></returns>
        public static EContractInfoResponseDto GetEContractInfo(string contractNumber, BaseRequest baseres)
        {
            string ordernum = "";
            if (!string.IsNullOrEmpty(contractNumber))
            {
                ordernum = "/" + contractNumber;
            }
            RestRequest request = new RestRequest("/v1/econtract" + ordernum, Method.GET);
            SetHeaders(request, baseres);
            //request.AddQueryParameter("", contractNumber);

            var dto = RestHelper.Execute<EContractInfoResponseDto>(EContractConstValue.BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 线下签约状态通知
        /// 按合同编号上传线下签署材料，同时更新合同状态为已签署
        /// </summary>
        /// <param name="material">签署图片的base64编码 </param>
        /// <param name="contractNumber">电子合同编号</param>
        public static CommonResponsetDto OfflineSignStateNotice(string material, string contractNumber, BaseRequest baseres)
        {
            RestRequest request = new RestRequest("/v1/econtract/signature", Method.POST);
            SetHeaders(request, baseres);
            request.AddQueryParameter("contractNumber", contractNumber);
            request.AddQueryParameter("material", material);
            var dto = RestHelper.Execute<CommonResponsetDto>(EContractConstValue.BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 作废电子合同
        /// 按合同编号作废合同，如果已签署，则发送短信给游客，成功取消将会通过回调机制通知；否则直接取消
        /// </summary>
        /// <param name="contractNumber">电子合同编号</param>
        /// <returns></returns>
        public static CommonResponsetDto InvalidateEContract(string contractNumber, BaseRequest baseres)
        {
            string ordernum = "";
            if (!string.IsNullOrEmpty(contractNumber))
            {
                ordernum = "/" + contractNumber;
            }
            RestRequest request = new RestRequest("/v1/econtract/invalidate" + ordernum, Method.POST);
            SetHeaders(request, baseres);
            var dto = RestHelper.Execute<CommonResponsetDto>(EContractConstValue.BaseUrl, request);
            return dto;
        }

        /// <summary>
        ///  作废补充协议合同
        ///  按合同编号作废补充协议合同，如果已签署，则发送短信给游客，成功取消将会通过回调机制通知；否则直接取消
        /// </summary>
        /// <param name="contractNumber">电子合同编号</param>
        public static CommonResponsetDto InvalidateSupplementAgreement(string contractNumber, BaseRequest baseres)
        {
            RestRequest request = new RestRequest("/v1/econtract/sa/invalidate", Method.POST);
            SetHeaders(request, baseres);
            request.AddQueryParameter("contractNumber", contractNumber);

            var dto = RestHelper.Execute<CommonResponsetDto>(EContractConstValue.BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 重发短信  按合同编号重新发送签署短信
        /// </summary>
        /// <param name="contractNumber">电子合同编号</param>
        /// <returns></returns>
        public static CommonResponsetDto RestSendSms(string contractNumber, BaseRequest baseres)
        {
            RestRequest request = new RestRequest("/v1/econtract/resend", Method.POST);
            SetHeaders(request, baseres);
            request.AddQueryParameter("contractNumber", contractNumber);

            var dto = RestHelper.Execute<CommonResponsetDto>(EContractConstValue.BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 补充协议重发签署短信  按合同编号重新发送签署短信
        /// </summary>
        /// <param name="contractNumber">电子合同编号</param>
        /// <returns></returns>
        public static CommonResponsetDto RestSendSmsSupplementAgreement(string contractNumber, BaseRequest baseres)
        {
            RestRequest request = new RestRequest("/v1/econtract/sa/resend", Method.POST);
            SetHeaders(request, baseres);
            request.AddQueryParameter("contractNumber", contractNumber);

            var dto = RestHelper.Execute<CommonResponsetDto>(EContractConstValue.BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 上传签章  按旅行社许可证号上传签章
        /// </summary>
        /// <param name="stamp">签章图片的base64编码</param>
        /// <param name="travelAgencyLicenseNumber">旅行社许可证号</param>
        /// <returns></returns>
        public static CommonResponsetDto UploadStamp(string stamp, string travelAgencyLicenseNumber, BaseRequest baseres)
        {
            RestRequest request = new RestRequest("/v1/econtract/stamp", Method.POST);
            SetHeaders(request, baseres);
            //TODO:base64
            request.AddQueryParameter("travelAgencyLicenseNumber", travelAgencyLicenseNumber);
            request.AddQueryParameter("stamp", stamp);

            var dto = RestHelper.Execute<CommonResponsetDto>(EContractConstValue.BaseUrl, request);
            return dto;
        }


        /// <summary>
        /// 组织header信息
        /// </summary>
        /// <returns></returns>
        private static void SetHeaders<t>(RestRequest request, t dto)
        {
            string SourceAppId=""; string SignKey = "";string KeyAppId = "";string Version = "";
            Type type = typeof(t);
            PropertyInfo pro = type.GetProperty("SourceAppId");
            PropertyInfo pro2 = type.GetProperty("KeyAppId");
            PropertyInfo pro3 = type.GetProperty("Version");
            PropertyInfo pro4 = type.GetProperty("SignKey");
            if (pro != null)
            {
                SourceAppId= pro.GetValue(dto).ToString();
            }
            if (pro2 != null)
            {
                KeyAppId = pro2.GetValue(dto).ToString();
            }
            if (pro3 != null)
            {
                Version = pro3.GetValue(dto).ToString();
            }
            if (pro4 != null)
            {
                SignKey = pro4.GetValue(dto).ToString();
            }
            string signKey = SignKey;
            long timestamp = ConvertHelper.GetTimeStamp();
            string signStr = signKey + timestamp;
            request.AddHeader("Content-Type", "application/json;charset=utf-8");
            request.AddHeader("x-12301-source", SourceAppId); // ERP授权key
            request.AddHeader("x-12301-key", KeyAppId);        // 授权访问key
            request.AddHeader("x-12301-version", Version); // 填写应用的版本号信息，1.1.1
            request.AddHeader("x-12301-timestamp", timestamp.ToString()); // 时间戳
            request.AddHeader("x-12301-signature", DEncryptHelper.GetMD5ToBase64(signStr)); // 签名字符串

            //request.AddHeader("Content-Type", "application/json;charset=utf-8");
            //request.AddHeader("x-12301-key", "d9d7189d8552a3003369f98cf5ca2e9a");        // 授权访问key
            //request.AddHeader("x-12301-source", "d9d7189d8552a3003369f98cf5ca2e9a"); // ERP授权key
            //request.AddHeader("x-12301-signature", "NTJhYzgyMDhkODIyMWU1OGY5YTkxOGJjMzkxMTU2MzM="); // 签名字符串
            //request.AddHeader("x-12301-version", EContractConstValue.Version); // 填写应用的版本号信息，1.1.1
            //request.AddHeader("x-12301-timestamp", "1542967531420"); // 时间戳
        }
    }
}
